﻿using BasicsClassLibrary.Tool.Configure;
using Consul;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace WebApplication3.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class testController : ControllerBase
    {
        private static int index = 0;
        [HttpGet]
        [Route("Contents")]
        public IActionResult Contents()
        {
            string url = $"http://{AppHelper.ReadAppSettings("Consul", "serviceName")}/api/values";
            ConsulClient client = new ConsulClient(c=>
            {
                c.Address = new Uri(AppHelper.ReadAppSettings("Consul", "consulAddress"));
            }
            );
            var res = client.Agent.Services().Result.Response;
            Uri uris = new Uri(url);
            string groupname = uris.Host;
            var dic = res.Where(s => s.Value.Service.Equals(groupname, StringComparison.OrdinalIgnoreCase)).ToArray();
            //轮询读取
            return Content(Newtonsoft.Json.JsonConvert.SerializeObject(dic[index++ % dic.Length].Value));
        }

        
      
    }
}
